package org.exia.minisig.application;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.filechooser.FileSystemView;

public class Utils {
	public static BufferedImage scaleImage(int WIDTH, int HEIGHT, Image image,float factor) {
			BufferedImage originalImage = (BufferedImage) image;
			int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
			
			int w = (int)(WIDTH * factor);
			int h = (int)(HEIGHT * factor);
			BufferedImage resizedImage = new BufferedImage(w, h, type);
			Graphics2D g = resizedImage.createGraphics();
			g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
			g.drawImage(originalImage, 0, 0, w, h, null);
			g.dispose();
			return resizedImage;
	}
	public static String applicationFolder(){
	 	FileSystemView fsv = FileSystemView.getFileSystemView();
		File f = fsv.getDefaultDirectory();
		return f + "\\miniSIG\\";
	}
	public static Image loadImage(String path){
		try {
			File img = new File(path);
			return ImageIO.read(img);
		} catch (IOException e) {
			LogFile.setEntry("Erreur chagement image :" + path);
			return null;
		}
	}
	
	public static Image setIconSize(Image original, int witdh, int height){
		BufferedImage originalImg = (BufferedImage) original;
		int type = (originalImg.getTransparency() == Transparency.OPAQUE) ?
			    BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
		BufferedImage resizedImage = new BufferedImage(witdh, height, type);
		Graphics2D g = resizedImage.createGraphics();
		g.drawImage(original, 0, 0, witdh, height, null);
		g.dispose();
		g.setComposite(AlphaComposite.Src);
		g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		return resizedImage;
	}
	public static Image loadImage(URL resource) {
		try {
			File img = new File(resource.getPath());
			return ImageIO.read(img);
		} catch (IOException e) {
			LogFile.setEntry("Erreur chagement image :" + resource);
			return null;
		}
	}
}
